#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@#
compile="no"
DO_VALENCE_ANALISIS="no"
		# Optional:
		Do_Zq_democratic="no"
	Do_Calc_Amp="no"
	DO_Zm_PROM_EVEN_DEMO="no" 
# Chose iprop=16 from now on if you're dealing with the ensemble 000+ (b=190)
#       iprop=0                                                  ++++ 
### Chose pm=a 
	Do_prom_pm="no"
        Do_ChiralVal="no"
		Do_ZP="no"
		Do_ZALL_ChiralLimit="no"
	Do_prom_pm_chiral="no"
		# Optional:
		Do_RGI_valence="no"
		Do_p2_val_fit="no"
### Chose e=0
DO_CHIRAL_SEA="no"
DO_PROM_EVEN_DEMO="no"
DO_RGI="no"
DO_P2FIT="no"

DO_RENO_SEA_QUARK_MASS="yes"

source ~/workdir/root/bin/thisroot.sh

##########################
#Beta (beta*100)
#beta100=190
#beta100=195
#beta100=210
beta100=${1}
beta=$(echo "scale=2; $beta100/100" | bc -l)


#############################

################################
## Ensemble List
if [ $beta100 -eq 195 ]
then
#eList=(1 2 3 4 7 8 )
eList=(1 2 3 7 8)
fi
if [ $beta100 -eq 190 ]
then
#eList=("A1")
eList=("A3")
fi
if [ $beta100 -eq 210 ]
then
eList=(2 3 4 5)
fi

#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#   For CHIRAL SEA analysis steps choose e=0
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
eList=("${2}")

#Choose theta: p, m
#pmList=("p" "m")
#pmList=("m")
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#   For steps after p/m average choose pm=a
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
pmList=("${3}")
###################################

#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@#
#@@   FIXED INPUT VARIABLES    
#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@#


###################################
# Directories
dirreno="/home/nucave/workdir/lattice/RENORMALIZATION/reno"

dir="$dirreno/NF4/Z2F/"
dirinput="$dirreno/NF4/Z2F/INPUT"

dirdata="/home/nucave/workdir/lattice/RENORMALIZATION/"
######################################

# Nclust
Nclust=16
#Gauge Fixing and Gauge Action
gauge_fixing="landau"
gauge_action="iwasaki"

# Fermionic Action
Nf=4
action="TwistedMass"

#Nr=2 -> r0 & r1
Nr=2

#@@   depending on beta          
#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@#

#####################################
#Gamma and Momentum order convention
#WARNIG: No correr a la vez scripts para diferentes definiciones de gammas o momentos

if [ $beta100 -eq 190 ] || [ $beta100 -eq 210 ]
then
gamma="TWISTED"
momentum_order="TYZX"
convention="TW"
twisted_basis=1
fi

if [ $beta100 -eq 195 ]
then
gamma="APE"
momentum_order="TYZX"
convention="APE"
twisted_basis=0
fi

TEST=0
#TEST=1
if [ $TEST -eq 1 ]
then 
gamma="TWISTED"
momentum_order="TXYZ"
convention="TW"
fi

# Original Francesco format
#gamma="TWISTED"
#momentum_order="TXYZ"
#convention="HUYGENS-CINECA"


########################################

# democratic momenta set

democratic_set="yes"
# The standard one corresponds to iprop=0
# For the even democratic promedium (000+) set iprop=16
iprop=${4}
list_democratic_prop=(0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15)
list_democratic_pz=(+ + + + + + + + - - - - - - - - 0)
list_democratic_py=(+ + + + - - - - + + + + - - - - 0)
list_democratic_px=(+ + - - + + - - + + - - + + - - 0) 
list_democratic_pt=(+ - + - + - + - + - + - + - + - +)

#######################################

# L^3xT=V
if [ $beta100 -eq 190 ]
then
 L=24
 T=48
fi 
if [ $beta100 -eq 195 ]
then
 L=24
 T=48
fi
if [ $beta100 -eq 210 ]
then
 L=32
 T=64
fi

##################################
# Oa2 corrections
Oa2List=("uncor" "Oa2g2" "Oa2g2b") 
#Oa2List=("Oa2g2b") #PETROS-TEST

NOa2=${#Oa2List[*]}

if [ $beta100 -eq 190 ]; then  Uplaq=0.573283; fi
if [ $beta100 -eq 195 ]; then  Uplaq=0.5842; fi
if [ $beta100 -eq 210 ]; then  Uplaq=0.6133375; fi 

##########################################

######################################3
# ainv for RGI
if [ $beta100 -eq 190 ]; then  ainv=2.19 ; fi
if [ $beta100 -eq 195 ]; then  ainv=2.50 ; fi
if [ $beta100 -eq 210 ]; then  ainv=3.23 ; fi

############## This will be the common case

if [ $Do_RGI_valence = "no" ] 
then
rgiList=(\"\")
fi

########## Just for intermediate checks:
if [ $Do_RGI_valence = "yes" ] 
then
rgiList=(\"\")

# If I have computed RGI also on the valence I can read directly these rgi files and don't do the RGI on the sea.
#rgiList=("_RIp_ainv")

fi


##############################################3


##################################
## p2 FIT

# p2 cuts
if [ $DO_VALENCE_ANALISIS = "yes" ]
then
cut=100 #PETROS-TEST
else
cut=0.28
fi
p2min_lin=1.2
p2max_lin=2.2


if [ $beta100 -eq 195 ]
then
p2min_av=1.8
p2max_av=2.0
fi

if [ $beta100 -eq 210 ]
then
p2min_av=1.1
p2max_av=1.23
fi
if [ $beta100 -eq 190 ]
then
p2min_av=2.4
p2max_av=2.7
fi
######################################

#####################################
# Momenta
## $dirinput/mom file

if [ $beta100 -eq 190 ]
then
Nmom=492
Nmom_eq=135
fi

if [ $beta100 -eq 195 ]
then
Nmom=140
Nmom_eq=55

fi
if [ $beta100 -eq 210 ]
then
Nmom=492
Nmom_eq=135
fi


echo "1 0 0 0" > $dirinput/mom_${beta100}.dat # boundary conditions
echo "2" >> $dirinput/mom_${beta100}.dat # Nindexmom
if [ $Nmom -eq 140 ]
then
echo "0 3 0 2 0 2 0 2" >> $dirinput/mom_${beta100}.dat
echo "4 7 2 3 2 3 2 3" >> $dirinput/mom_${beta100}.dat
fi
if [ $Nmom -eq 492 ]
then
echo "0 3 0 2 0 2 0 2" >> $dirinput/mom_${beta100}.dat
echo "4 9 2 5 2 5 2 5" >> $dirinput/mom_${beta100}.dat
fi

#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@#

if [ $compile = "yes" ]
then
echo "Compiling..."

###### If you're going to launch a script don't compile. 
###### You may have different scripts compiling the same program
###### Compile before or do it just the first time.

sh compile.sh Calc_Zq_MAGRO_ALLAVERAGE Calc_Zq_MAGRO_ALLAVERAGE
sh compile.sh Calc_FF_AmpGF_and_Z_MAGRO_ALLAVERAGE Calc_FF_AmpGF_and_Z_MAGRO_ALLAVERAGE
sh compile.sh  Z_PROM_r_Gamma_and_QAVERAGE Z_PROM_r_Gamma_and_QAVERAGE
sh compile.sh Z_PROM_pm Z_PROM_pm
sh compile.sh Z_PROM_pm_CHIRAL_ZbyZ Z_PROM_pm_CHIRAL_ZbyZ
sh compile.sh ZRGI ZRGI

fi


################################################
#########################
# VALENCE ANALYSIS
#########################
###############################################

if [ $DO_VALENCE_ANALISIS = "yes" ]
then


#@@@@@@@@@@@ BEGIN ENSEMBLE LIST

for e in  ${eList[*]}
do
		echo $e
                #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@#
                #@@  MASSES TO BE ANALYSED  @@#
                #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@#

                if [ $beta100 -eq 190 ]
                then
                        if [ $e = "A1" ]; then Nmass=6; fi
			if [ $e = "A2" ]; then Nmass=6; fi
                        if [ $e = "A3" ]; then Nmass=6; fi
                        if [ $e = "A4" ]; then Nmass=6; fi
                fi


                if [ $beta100 -eq 195 ]
                then
                        if [ $e -eq 1 ] || [ $e -eq 2 ] || [ $e -eq 7 ] || [ $e -eq 8 ] ; then Nmass=5 ; fi
                        if [ $e -eq 3 ] || [ $e -eq 4 ] ; then Nmass=8 ; fi
                fi

                if [ $beta100 -eq 210 ]
                then
			if [ $e -eq 2 ]; then Nmass=7; fi
                        if [ $e -eq 3 ]; then Nmass=7; fi
                        if [ $e -eq 5 ]; then Nmass=6; fi
                        if [ $e -eq 4 ]; then Nmass=6; fi
                fi

###((((((((((((((((((((((((())))))))))))))))))))))))))))###
##              Compute Amputed Green Function 
## 		and RCs
## 		for the Oa2 list
###((((((((((((((((((((((((())))))))))))))))))))))))))))###
	if [ $Do_Zq_democratic = "yes" ] || [ $Do_Calc_Amp = "yes" ] 
	then
#@@@@@@@@@@@@@@ BEGIN PM LIST
	for pm in ${pmList[*]}
	do
		epm=${e}${pm}
		echo $epm 
                #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@#
                #@@  Configurations to be read:
		#@@  conf=conf_ini+i*jump with 
		#@@      i=0,1,...Nconf-1
		#@@ 	 Nconf=Nex*Nclust
                #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@#

		Nskip=0;
                if [ $beta100 -eq 190 ]
                then

	                #if [ $epm = "A1m" ] ; then conf_ini=129;  jump=6; Nex=15; fi
			if [ $epm = "A1m" ] ; then conf_ini=129;  jump=6; Nex=18; fi
         	      	if [ $epm = "A1p" ] ; then conf_ini=499 ; jump=6; Nex=15; fi
			if [ $epm = "A2m" ] ; then conf_ini=276;  jump=6; Nex=15; fi
                        if [ $epm = "A2p" ] ; then conf_ini=499;  jump=6; Nex=15; fi
                        if [ $epm = "A3m" ] ; then conf_ini=198 ; jump=6 ; Nex=15; fi
                        if [ $epm = "A3p" ] ; then conf_ini=499 ; jump=6 ; Nex=15; fi
                        if [ $epm = "A4m" ] ; then conf_ini=499 ; jump=6; Nex=15; fi
                       # if [ $epm = "A4p" ] ; then conf_ini=499 ; jump=4; Nex=20; fi
			if [ $epm = "A4p" ] ; then conf_ini=499 ; jump=4; Nex=15; fi


                fi

	        if [ $beta100 -eq 195 ]
        	then

		        if [ $e -eq 1 ] ; then  Nex=19 ; jump=4; fi
		        if [ $e -eq 2 ] ; then  Nex=22 ; jump=4; fi
        		if [ $e -eq 3 ] ; then  Nex=22 ; jump=4; fi
		        if [ $e -eq 4 ] ; then  Nex=21 ; jump=4; fi
#	        	if [ $e -eq 7 ] ; then  Nex=23 ;fi
#		        if [ $e -eq 8 ] ; then  Nex=19 ;fi # We have more statistic
		
			if [ $epm = "1m" ] ; then conf_ini=118 ;fi
			if [ $epm = "1p" ] ; then conf_ini=102 ;fi

			if [ $epm = "2m" ] ; then conf_ini=105 ;fi
			if [ $epm = "2p" ] ; then conf_ini=384 ;fi

			if [ $epm = "3m" ] ; then conf_ini=100 ;fi
			if [ $epm = "3p" ] ; then conf_ini=241 ;fi

# WARNING WARNING e=4 There are corrupted confs!
			if [ $epm = "4m" ] ; then conf_ini=459 ; Nskip=13; skipList="535 739 783 799 923 1167 1327 1419 1591 1603 1675 1691 1823"; fi
			if [ $epm = "4p" ] ; then conf_ini=172 ;fi

			if [ $epm = "7m" ] ; then conf_ini=1454 ; jump=2;  Nex=47;fi
			if [ $epm = "7p" ] ; then conf_ini=102 ;  jump=2; Nex=46; fi

			if [ $epm = "8m" ] ; then conf_ini=127 ; jump=2; Nex=47; fi
			if [ $epm = "8p" ] ; then conf_ini=326 ; jump=2; Nex=39; fi
		fi
	        if [ $beta100 -eq 210 ]
	        then

			if [ $epm = "2m" ] ; then jump=4; Nex=12; conf_ini=260; Nskip=1 ; skipList="816";  fi
			if [ $epm = "2p" ] ; then jump=1; Nex=18; conf_ini=100; fi
			if [ $epm = "3p" ] ; then jump=1; Nex=15; conf_ini=100; fi
                        if [ $epm = "3m" ] ; then jump=1; Nex=22; conf_ini=100; fi
                        if [ $epm = "4p" ] ; then jump=1; Nex=13; conf_ini=100; fi
                        if [ $epm = "4m" ] ; then jump=1; Nex=23; conf_ini=100; fi
                        if [ $epm = "5p" ] ; then jump=1; Nex=14; conf_ini=100; fi
			if [ $epm = "5m" ] ; then jump=1; Nex=18; conf_ini=100; fi

		fi
		if [ $TEST -eq 1 ] ; then conf_ini=503; Nclust=14; Nex=2; jump=2 ; fi
		
				#### TESTS:
				        #if [ $beta100 -eq 195 ]; then
					        #if [ $e -eq 2 ] ; then 
	                				#368 confs 
					        	        #conf_ini=105; Nclust=23; Nex=16; jump=4
							#cineca & huygens-cineca test with 30 confs
								#Nmass=1; conf_ini=501;Nclust=2;Nex=2;jump=4
							#ape test
								#Nmass=5; conf_ini=501; Nclust=14; Nex=2; jump=4
						#fi
					#fi


		#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@#
		#@@  kappa_sea                       
		#@   (only for name construction)
		#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@#

		if [ $beta100 -eq 195 ]
		then
                	if [ $epm = "1m" ] ; then kappa_sea="0.160754" ;fi
        	        if [ $epm = "1p" ] ; then kappa_sea="0.162145" ;fi
	
        	        if [ $epm = "2m" ] ; then kappa_sea="0.161229" ;fi
	        	if [ $epm = "2p" ] ; then kappa_sea="0.160826" ;fi

        	        if [ $epm = "3m" ] ; then kappa_sea="0.161229" ;fi
	        	if [ $epm = "3p" ] ; then kappa_sea="0.160826" ;fi

                	if [ $epm = "4m" ] ; then kappa_sea="0.161095" ;fi
	                if [ $epm = "4p" ] ; then kappa_sea="0.16087" ;fi

		        if [ $epm = "7m" ] ; then kappa_sea="0.160681" ;fi
        		if [ $epm = "7p" ] ; then kappa_sea="0.161925" ;fi

	                if [ $epm = "8m" ] ; then kappa_sea="0.160681" ;fi
	                if [ $epm = "8p" ] ; then kappa_sea="0.161925" ;fi

		fi
                if [ $beta100 -eq 210 ]
	        then
			if [ ${epm} = "2m" ] ; then kappa_sea="0.156157" ; fi
			if [ ${epm} = "2p" ] ; then kappa_sea="0.156042" ; fi
        	        if [ ${epm} = "3m" ] ; then kappa_sea="0.156209" ; fi
                	if [ ${epm} = "3p" ] ; then kappa_sea="0.156017" ; fi
			if [ ${epm} = "4m" ] ; then kappa_sea="0.156250" ; fi
			if [ ${epm} = "4p" ] ; then kappa_sea="0.155983" ; fi
                        if [ ${epm} = "5m" ] ; then kappa_sea="0.156291" ; fi
	                if [ ${epm} = "5p" ] ; then kappa_sea="0.155949" ; fi
        	fi
                if [ $beta100 -eq 190 ]
	        then
        	        if [ ${epm} = "A1p" ] ; then kappa_sea="0.163206" ; fi

			if [ ${epm} = "A3p" ] ; then kappa_sea="0.164537" ; fi
                        if [ ${epm} = "A3m" ] ; then kappa_sea="0.162260" ; fi
                        if [ ${epm} = "A4p" ] ; then kappa_sea="0.164817" ; fi
			if [ ${epm} = "A4m" ] ; then kappa_sea="0.162113" ; fi

  		fi


		#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
		#@@ INPUT OUTPUT DIRECTORIES
		#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
	
		if [ $convention = "APE" ]
		then
	              dir_input="$dirdata/DATA/INPUT-APE/${action}Nf${Nf}/b$beta100/${e}${pm}/s0ft/"
		      dir_output="$dirdata/DATA/OUTPUT-APE/${action}Nf${Nf}/b$beta100/Z2F/${e}${pm}/"
			# WARNING WARNING: checks
			#dir_input="$dirdata/DATA/INPUT-APE/${action}Nf${Nf}/b$beta100/${e}${pm}/s0ft/"
			#dir_output="$dirdata/DATA/OUTPUT-APE/${action}Nf${Nf}/b$beta100/Z2F/${e}${pm}-28confs/"
		else if [ $convention = "HUYGENS-CINECA" ]
		then
			dir_input="$dirdata/DATA/INPUT-HUYGENS-CINECA/${action}Nf${Nf}/b$beta100/${e}${pm}/s0ft/"
                        dir_output="$dirdata/DATA/OUTPUT-HUYGENS-CINECA/${action}Nf${Nf}/b$beta100/Z2F/${e}${pm}-28confs/"

		else
			if [ $TEST -eq 1 ]
			then
                              dir_input="$dirdata/DATA/INPUT/${action}Nf${Nf}/b$beta100/${e}${pm}/s0ft-28/"
                              dir_output="$dirdata/DATA/OUTPUT/${action}Nf${Nf}/b$beta100/Z2F/${e}${pm}-28/"

			else
		              dir_input="$dirdata/DATA/INPUT/${action}Nf${Nf}/b$beta100/${e}${pm}/s0ft/"
			      dir_output="$dirdata/DATA/OUTPUT/${action}Nf${Nf}/b$beta100/Z2F/${e}${pm}/"
			fi
		
		fi
		fi
				# Old version no-qav
				#mkdir -p -v $dir_output/Z_ALLP
				#mkdir -p -v $dir_output/FF_ALLP
        			#mkdir -p -v $dir_output/LAMBDA_ALLP
				#for i in `seq 0 $Nmom`; do imom=`printf %03d $i`; mkdir -p -v $dir_output/p$imom ; done
		
		#mkdir -p -v $dir_output/QAV/Z_ALLP
		#mkdir -p -v $dir_output/QAV/FF_ALLP
		#for i in `seq 0 $Nmom_eq`; do imom=`printf %03d $i`; mkdir -p -v $dir_output/QAV/p$imom ; done

		    ##########=======================================##########################
		if [ $Nskip -gt 0 ]
		then
		for skip in ${skipList[*]} ; do echo -n "-skip=$skip " >> __tmp_skip__ ; done 
		writeskip=$(more __tmp_skip__) 
		rm __tmp_skip__
		fi
                args="-gamma=${gamma} -momentum_order=${momentum_order} -L=$L -T=$T -e=$e -dir_input=$dir_input -conf_ini=${conf_ini} -jump=${jump} -Nclust=${Nclust} -Nex=${Nex} -Nmass=$Nmass -Nr=${Nr} -kappa_sea=${kappa_sea}  -NOa2=${NOa2} -gauge_fixing=${gauge_fixing} -gauge_action=${gauge_action} -beta=${beta} -Uplaq=${Uplaq} -Nskip=${Nskip} $writeskip"
                ##########=======================================##########################


                cd $dir/EXECUTABLES/

                if [ $Do_Zq_democratic = "yes" ]
                then
                        for iprop in `seq 0 1 14`
                        do
                                mkdir -p $dir_output/${list_democratic_pz[$iprop]}${list_democratic_py[$iprop]}${list_democratic_px[$iprop]}${list_democratic_pt[$iprop]}/QAV/FF_ALLP
                                mkdir -p $dir_output/${list_democratic_pz[$iprop]}${list_democratic_py[$iprop]}${list_democratic_px[$iprop]}${list_democratic_pt[$iprop]}/QAV/Z_ALLP 
                                for i in `seq 0 $Nmom_eq`; do imom=`printf %03d $i`; mkdir -p -v $dir_output/${list_democratic_pz[$iprop]}${list_democratic_py[$iprop]}${list_democratic_px[$iprop]}${list_democratic_pt[$iprop]}/QAV/p$imom ; done
				echo " $args   -dir_output=$dir_output/${list_democratic_pz[$iprop]}${list_democratic_py[$iprop]}${list_democratic_px[$iprop]}${list_democratic_pt[$iprop]} -set_p=${list_democratic_pz[$iprop]}1,${list_democratic_py[$iprop]}1,${list_democratic_px[$iprop]}1,${list_democratic_pt[$iprop]}1 -iprop=${iprop} -beta100=${beta100}
"
				./Calc_Zq_MAGRO_ALLAVERAGE  $args   -dir_output=$dir_output/${list_democratic_pz[$iprop]}${list_democratic_py[$iprop]}${list_democratic_px[$iprop]}${list_democratic_pt[$iprop]} -set_p=${list_democratic_pz[$iprop]}1,${list_democratic_py[$iprop]}1,${list_democratic_px[$iprop]}1,${list_democratic_pt[$iprop]}1 -iprop=${iprop} -beta100=${beta100} -twisted_basis=${twisted_basis} 
			done
		fi
		if [ $Do_Calc_Amp = "yes" ] 
		then 
			 if [ $democratic_set = "yes" ]
			 then

				 mkdir -p $dir_output/${list_democratic_pz[$iprop]}${list_democratic_py[$iprop]}${list_democratic_px[$iprop]}${list_democratic_pt[$iprop]}/QAV/FF_ALLP
				 mkdir -p $dir_output/${list_democratic_pz[$iprop]}${list_democratic_py[$iprop]}${list_democratic_px[$iprop]}${list_democratic_pt[$iprop]}/QAV/Z_ALLP 
				 for i in `seq 0 $Nmom_eq`; do imom=`printf %03d $i`; mkdir -p -v $dir_output/${list_democratic_pz[$iprop]}${list_democratic_py[$iprop]}${list_democratic_px[$iprop]}${list_democratic_pt[$iprop]}/QAV/p$imom ; done
				 echo "$args -dir_output=$dir_output/${list_democratic_pz[$iprop]}${list_democratic_py[$iprop]}${list_democratic_px[$iprop]}${list_democratic_pt[$iprop]} -set_p=${list_democratic_pz[$iprop]}1,${list_democratic_py[$iprop]}1,${list_democratic_px[$iprop]}1,${list_democratic_pt[$iprop]}1 -iprop=${iprop} -beta100=${beta100}"
				
				./Calc_FF_AmpGF_and_Z_MAGRO_ALLAVERAGE $args -dir_output=$dir_output/${list_democratic_pz[$iprop]}${list_democratic_py[$iprop]}${list_democratic_px[$iprop]}${list_democratic_pt[$iprop]} -set_p=${list_democratic_pz[$iprop]}1,${list_democratic_py[$iprop]}1,${list_democratic_px[$iprop]}1,${list_democratic_pt[$iprop]}1 -iprop=${iprop} -beta100=${beta100}
			 fi
		 fi



		done
#@@@@@@@@@@@@@@ END PM LIST
 
	fi 
####### END Do_Calc_Amp 


        if  [ $DO_Zm_PROM_EVEN_DEMO = "yes" ]
        then
	for pm in ${pmList[*]}
	do
        iprop=16
       if [ $convention = "APE" ]
       then
                dir_output="$dirdata/DATA/OUTPUT-APE/${action}Nf${Nf}/b$beta100/Z2F/${e}${pm}"
       else
                dir_output="$dirdata/DATA/OUTPUT/${action}Nf${Nf}/b$beta100/Z2F/${e}${pm}"
       fi


        mkdir -p -v $dir_output//000+/QAV/Z_ALLP
       for i in `seq 0 $Nmom_eq`; do imom=`printf %03d $i`; mkdir -p -v ${dir_output}//000+/QAV/p$imom ; done


       cd $dir/EXECUTABLES/

        for Oa2 in ${Oa2List[*]}
        do
              args=" -beta100=${beta100} -momentum_order=${momentum_order} -L=$L -T=$T -Nclust=${Nclust} -dir=$dir_output -Nprop=8 -output_name=000+  -list_prop=0,2,4,6,8,10,12,14  -Oa2=${Oa2} -Nmass=${Nmass}"
              time ./Zm_PROM_DEMO  $args
              echo " =============================================="
        done

	done # end pm-list

        fi


###((((((((((((((((((((((((())))))))))))))))))))))))))))###
##              p/m AVERAGE before chiral limit
###((((((((((((((((((((((((())))))))))))))))))))))))))))###

        if [ $Do_prom_pm = "yes" ] 
        then
       #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
       #@ p/m AVERAGE DIRECTORIES
       #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

       if [ $convention = "APE" ]
       then
                dir_output="$dirdata/DATA/OUTPUT-APE/${action}Nf${Nf}/b$beta100/Z2F/${e}"
       else
                dir_output="$dirdata/DATA/OUTPUT/${action}Nf${Nf}/b$beta100/Z2F/${e}"
       fi

	if [ $democratic_set = "yes" ]
	then
		dir_output_a=${dir_output}a/${list_democratic_pz[$iprop]}${list_democratic_py[$iprop]}${list_democratic_px[$iprop]}${list_democratic_pt[$iprop]}
	else
		dir_output_a=${dir_output}a
	fi

       mkdir -p -v ${dir_output_a}/QAV/Z_ALLP
       for i in `seq 0 $Nmom_eq`; do imom=`printf %03d $i`; mkdir -p -v ${dir_output_a}/QAV/p$imom ; done


       ##########=======================================##########################
       args="-momentum_order=${momentum_order} -L=$L -T=$T -Nclust=${Nclust} -Nmass=${Nmass}" 
       ##########=======================================##########################

       cd $dir/EXECUTABLES/

	for Oa2 in ${Oa2List[*]}
        do
		# Note that the p/m/a directory name is constructed in the .c program 
		args="-momentum_order=${momentum_order} -L=$L -T=$T -Nclust=${Nclust} -Nmass=${Nmass}  -dir=$dir_output  -set_p=${list_democratic_pz[$iprop]}1,${list_democratic_py[$iprop]}1,${list_democratic_px[$iprop]}1,${list_democratic_pt[$iprop]}1  -iprop=$iprop -Oa2=${Oa2} -beta100=${beta100}"
		echo $args
	        time ./Z_PROM_pm $args 
	done

	fi # END Do_prom_pm 

###((((((((((((((((((((((((())))))))))))))))))))))))))))###
##              Valence Chiral Limit
###((((((((((((((((((((((((())))))))))))))))))))))))))))###

	if [ $Do_ChiralVal = "yes" ]
	then

	   ##@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@2
	   #@@ WRITE MASS INPUT FILES
	   #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
           for pm in "p" "m" "a" 
	   do
		epm=${e}${pm}
                if [ $beta100 -eq 190 ]
                then
			ZAPREV=0.748
                        if [ $e = "A1" ] ||  [ $e = "A2" ] || [ $e = "A3" ] || [ $e = "A4" ]
                        then
                                muList=(0.0060 0.0080 0.0120 0.0170 0.0210 0.0260)
			fi

                        if [ $epm = "A1p" ];then mPCAC=0.01629; fi
			if [ $epm = "A1m" ];then mPCAC=0.01588; fi
			if [ $epm = "A1a" ];then mPCAC=0.01609; fi

                        if [ $epm = "A2p" ];then mPCAC=0.01552; fi
                        if [ $epm = "A2m" ];then mPCAC=0.01536; fi
                        if [ $epm = "A2a" ];then mPCAC=0.01544; fi

                        if [ $epm = "A3p" ];then mPCAC=0.01525; fi
                        if [ $epm = "A3m" ];then mPCAC=0.01469; fi
                        if [ $epm = "A3a" ];then mPCAC=0.01497; fi

                        if [ $epm = "A4p" ];then mPCAC=0.0142; fi
                        if [ $epm = "A4m" ];then mPCAC=0.0147; fi
                        if [ $epm = "A4a" ];then mPCAC=0.01445; fi


                        if [ $epm = "A1p" ]; then MPSList=(0.268120752 0.273678855 0.286937707 0.305098627 0.319859691 0.338077073 0.278840807 0.291457274 0.309043702 0.32347193 0.341377957 0.302972885 0.319507306 0.333291525 0.350567241 0.334955306 0.348031935 0.364580305 0.360625206 0.376658069 0.392125341 );fi
                        if [ $epm = "A1m" ]; then MPSList=(0.227802249 0.234830217 0.249786633 0.268922584 0.284078627 0.30258673 0.241206044 0.255195767 0.273487899 0.288133611 0.306129742 0.267640823 0.284494794 0.29824138 0.315321118 0.299926794 0.312751552 0.328871716 0.324963282 0.340434077 0.355207591
 );fi


			if [ $epm = "A2p" ]; then MPSList=(0.267229315 0.273170904 0.287141557 0.305988268 0.321164316 0.339785076 0.278659948 0.291911167 0.310124191 0.324935897 0.34321821 0.303947782 0.321014045 0.335128372 0.35273204 0.336909039 0.350266283 0.367097069 0.36311123 0.379400286 0.395100708); fi
                        if [ $epm = "A2m" ]; then MPSList=(0.224203831 0.231219298 0.246401662 0.265887977 0.281275929 0.300013133 0.237639537 0.251881371 0.270508154 0.285370339 0.3035813 0.264568356 0.281720803 0.295663247 0.312944347 0.29741693 0.310423501 0.326738573 0.322812903 0.338477587 0.353444678) ; fi

                        if [ $epm = "A3p" ]; then MPSList=(0.265216934 0.271531587  0.286146332 0.30558633 0.321123553  0.340114756 0.277357728  0.291219721 0.31002186 0.325202175 0.343866774 0.303832602 0.321482491 0.335973696 0.353973709 0.337964303 0.351704869 0.368939947 0.364936478 0.381631347 0.397734775); fi
                        if [ $epm = "A3m" ]; then MPSList=( 0.217781488 0.224794155  0.240061987  0.259597882 0.274955528 0.293588959 0.231201294 0.245534713 0.264234336 0.279092049 0.297240315 0.258339699 0.275603882 0.289577184 0.306845052 0.291453023 0.30452239 0.320863707 0.316996687 0.332712234 0.347755052) ; fi

                        if [ $epm = "A4p" ]; then MPSList=(0.267607195  0.274105196 0.289091476  0.308918239 0.324696294  0.34391784  0.280074906  0.294250278 0.313389665 0.328783706 0.347656249 0.30709032 0.325002607 0.339664686 0.35783446 0.341680667 0.35555297 0.372920968 0.368892498 0.385697234 0.401887928);  fi
                        if [ $epm = "A4m" ]; then MPSList=(0.212383364 0.220162098 0.236317373 0.256514771 0.272275847 0.291343384 0.227169963 0.242205796 0.261450014 0.27664595 0.295153440 0.255502533 0.273170158 0.287398407 0.304931356 0.289317522 0.302575237 0.319106853 0.315195311 0.331055742 0.346198771 ); fi

                fi

                if [ $beta100 -eq 195 ]
                then
			ZAPREV=0.763
                        if [ $e -eq 1 ] || [ $e -eq 2 ] || [ $e -eq 7 ] || [ $e -eq 8 ] ; then muList=(0.0085 0.015 0.0203 0.0252 0.0298) ; fi
                        if [ $e -eq 3 ] || [ $e -eq 4 ] ; then muList=(0.0060 0.0085 0.0120 0.0150 0.0180 0.0203 0.0252 0.0298) ; fi
			
                        if [ $epm = "1p" ];then mPCAC=0.0195; fi # * mPCAC_val ! = mPCAC_sea
                        if [ $epm = "1m" ];then mPCAC=-0.0216; fi
                        if [ $epm = "1a" ];then mPCAC=0.0200; fi

                        if [ $epm = "2p" ];then mPCAC=0.0191; fi
                        if [ $epm = "2m" ];then mPCAC=-0.0213; fi
                        if [ $epm = "2a" ];then mPCAC=0.0200; fi

                        if [ $epm = "3p" ];then mPCAC=0.0162; fi
                        if [ $epm = "3m" ];then mPCAC=-0.0160; fi
                        if [ $epm = "3a" ];then mPCAC=0.0161; fi

                        if [ $epm = "4p" ];then mPCAC=0.0151; fi
                        if [ $epm = "4m" ];then mPCAC=-0.0146; fi
                        if [ $epm = "4a" ];then mPCAC=0.0149; fi

                        if [ $epm = "7p" ];then mPCAC=0.0181; fi # * mPCAC_val ! = mPCAC_sea
                        if [ $epm = "7m" ];then mPCAC=-0.0180; fi
                        if [ $epm = "7a" ];then mPCAC=0.0180; fi

                        if [ $epm = "8p" ];then mPCAC=0.0183; fi # * mPCAC_val ! = mPCAC_sea
                        if [ $epm = "8m" ];then mPCAC=-0.0194; fi
                        if [ $epm = "8a" ];then mPCAC=0.0187; fi




                        if [ $epm = "1p" ] ; then MPSList=(0.2955 0.3127 0.3290 0.3445 0.3591 0.3282 0.3434 0.3581 0.3721 0.3578 0.3719 0.3855 0.3856 0.3988 0.4116) ;fi
                        if [ $epm = "1m" ] ; then MPSList=(0.2553 0.2732 0.2895 0.3050 0.3195 0.2888 0.3037 0.3181 0.3318 0.3177 0.3313 0.3443 0.3444 0.3569 0.3690) ;fi

                        if [ $epm = "2p" ] ; then MPSList=(0.2764 0.2925 0.3079 0.3226 0.3365 0.3072 0.3216 0.3357 0.3491 0.3354 0.3489 0.3619 0.3621 0.3747 0.3870) ;fi
                        if [ $epm = "2m" ] ; then MPSList=(0.2587 0.2770 0.2937 0.3094 0.3240 0.2930 0.3082 0.3227 0.3366 0.3224 0.3362 0.3494 0.3494 0.3622 0.3745) ;fi

                        if [ $epm = "3p" ] ; then MPSList=(0.2550 0.2605 0.2701 0.2792 0.2886 0.2960 0.3117 0.3263 0.2658 0.2749 0.2836 0.2928 0.3000 0.3154 0.3298 0.2834 0.2918 0.3006 0.3076 0.3226 0.3366 0.2998 0.3084 0.3151 0.3298 0.3436 0.3167 0.3233 0.3376 0.3512 0.3297 0.3438 0.3572 0.3575 0.3705 0.3832) ;fi
                        if [ $epm = "3m" ] ; then MPSList=(0.1931 0.2062 0.2233 0.2370 0.2500 0.2597 0.2792 0.2966 0.2179 0.2335 0.2464 0.2587 0.2678 0.2865 0.3033 0.2477 0.2594 0.2709 0.2795 0.2973 0.3134 0.2705 0.2814 0.2896 0.3068 0.3223 0.2918 0.2997 0.3163 0.3314 0.3074 0.3235 0.3383 0.3389 0.3531 0.3668) ;fi

                        if [ $epm = "4p" ] ; then MPSList=(0.2434 0.2495 0.2598 0.2694 0.2792 0.2869 0.3031 0.3181 0.2552 0.26550 0.2742 0.2838 0.2913 0.3073 0.3220 0.2742 0.2830 0.2922 0.2994 0.3149 0.3294 0.2915 0.3004 0.3074 0.3226 0.3368 0.3091 0.3160 0.3307 0.3446 0.3227 0.3372 0.3509 0.3513 0.3646 0.3775) ;fi
                        if [ $epm = "4m" ] ; then MPSList=(0.2140 0.2229 0.2365 0.2483 0.2599 0.2687 0.2869 0.3034 0.2309 0.2433 0.2543 0.2653 0.2737 0.2912 0.3072 0.2545 0.2646 0.2749 0.2828 0.2994 0.3148 0.2741 0.2839 0.2914 0.3075 0.3224 0.2932 0.3005 0.3161 0.3305 0.3076 0.3228 0.3369 0.3372 0.3509 0.3640) ;fi

                        if [ $epm = "7p" ] ; then MPSList=(0.282638091 0.301027689 0.317993738 0.334000974 0.348939203 0.317524386 0.333327372 0.348493733 0.362795744 0.348338492 0.362892201 0.37670548  0.376950823 0.390356011 0.403416707) ;fi
                        if [ $epm = "7m" ] ; then MPSList=( 0.238559831  0.259201635 0.277090595 0.293612613 0.308888896 0.27693825  0.293093636 0.308342572 0.322625194 0.308164817  0.3225951  0.336226335  0.336393132  0.349512089  0.362209606) ;fi

                         if [ $epm = "8p" ] ; then MPSList=(0.282924605  0.301179812  0.318035273  0.333948003  0.348803474  0.317633898  0.333377744  0.348483981  0.362727689  0.348357462  0.362869163  0.376637287  0.376897984  0.390267435  0.403300018) ;fi
                         if [ $epm = "8m" ] ; then MPSList=(0.244623233  0.264032004  0.281343342  0.297516075  0.312557041  0.280883845  0.296597472  0.311564494  0.32565134   0.311263273  0.325426169  0.338871341  0.338964708  0.351904801  0.364429301) ;fi


                fi

                if [ $beta100 -eq 210 ]
                then
			ZAPREV=0.786

                        if [ $e -eq 2 ]
                        then
                                muList=(0.0013 0.0030 0.0080 0.0143 0.0195 0.0247 0.0298) 

                        fi

                        if [ $e -eq 3 ]
                        then
                                muList=(0.0025 0.0046 0.0090 0.0152 0.0201 0.0249 0.0297)

                        fi
                        if [ $e -eq 5 ]
                        then
                                muList=(0.0048  0.0078  0.0119  0.0190  0.0242  0.0293)
                        fi
                        if [ $e -eq 4 ]
                        then
                                muList=(0.0039 0.0064 0.0112 0.0184 0.0240 0.0295)
                        fi
			

			# CHECK en la tabla del POS no esta ni 2m/p ni 4m/p

                       if [ ${epm} = "2a" ];then mPCAC=0.0041; fi
                       if [ ${epm} = "2m" ];then mPCAC=-0.00403; fi
                       if [ ${epm} = "2p" ];then mPCAC=0.00421; fi

		       if [ ${epm} = "3a" ];then mPCAC=0.00572; fi
                       if [ ${epm} = "3m" ];then mPCAC=-0.00585; fi
                       if [ ${epm} = "3p" ];then mPCAC=0.00559; fi

                       if [ ${epm} = "4a" ];then mPCAC=0.00685; fi
                       if [ ${epm} = "4m" ];then mPCAC=-0.00682; fi
                       if [ ${epm} = "4p" ];then mPCAC=0.00684; fi

                       if [ ${epm} = "5a" ];then mPCAC=0.00822; fi
                       if [ ${epm} = "5m" ];then mPCAC=-0.00821; fi
                       if [ ${epm} = "5p" ];then mPCAC=0.00823; fi

	
                fi

                Nmass=${#muList[*]}
                Nmasscomb=${#MPSList[*]}
                Nmassm1=$(echo "$Nmass - 1 " | bc -l )

		#################################
		## INPUT OF THE PROGRAMS
		################################
                # mu_$epm.dat
                rm -f  $dirinput/mu_${e}${pm}_${beta100}.dat
                echo $Nmass > $dirinput/mu_${e}${pm}_${beta100}.dat
                for im in `seq 1 $Nmass`
                do
                                mass=${muList[$(echo " $im -1 " |bc -l )]}
                                echo $mass >> $dirinput/mu_${e}${pm}_${beta100}.dat
                done
		echo " ---> $dirinput/mu_${e}${pm}_${beta100}.dat file written"

                rm -f  $dirinput/MPS_${e}${pm}_${beta100}.dat
                for im in `seq 1 $Nmasscomb`
                do
                                MPS=${MPSList[$(echo " $im -1 " |bc -l )]}
                                echo $MPS >> $dirinput/MPS_${e}${pm}_${beta100}.dat
                done
		echo "  --> $dirinput/MPS_${e}${pm}_${beta100}.dat file written"	
	
                # mPCAC_$epm.dat
                rm -f $dirinput/mPCAC_${e}${pm}_${beta100}.dat
		echo $mPCAC > $dirinput/mPCAC_${e}${pm}_${beta100}.dat
                echo $ZAPREV >> $dirinput/mPCAC_${e}${pm}_${beta100}.dat
		echo "  --> $dirinput/mPCAC_${e}${pm}_${beta100}.dat  file written"        

	done # write pmList inputs

        echo "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
        echo "      ZP valence chiral fit         "
        echo "      p/m ensembles                 "
        echo "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"

	if [ $Do_ZP = "yes" ]
	then
        for Oa2print in ${Oa2List[*]}
	do
	for pm in "p" "m"
	do
		if [ $convention = "APE" ]
	        then

	        dir_output_doblebarra="\/home\/nucave\/workdir\/lattice\/RENORMALIZATION\/DATA\/OUTPUT-APE\/${action}Nf${Nf}\/b$beta100\/Z2F\/${e}${pm}\/"
		else
                dir_output_doblebarra="\/home\/nucave\/workdir\/lattice\/RENORMALIZATION\/DATA\/OUTPUT\/${action}Nf${Nf}\/b$beta100\/Z2F\/${e}${pm}\/"
		fi

		time for imom in `seq 0 $Nmom_eq`
		do
			echo "=================================================================="
			echo "=============>         $Oa2print p=$imom             <=================="
			if [ $beta100 -eq 210 ]
			then
	                sed "s/__MOMENTUM_ORDER__/$momentum_order/g;s/__BETA100__/$beta100/g;s/__L__/$L/g;s/__T__/$T/g;s/__NCLUST__/$Nclust/g;s/__DIROUTPUT__/$dir_output_doblebarra/g;s/__IMOM__/$imom/g;s/__ENSEMBLE__/${e}/g;s/__PMI__/${pm}/g;s/__Oa2PRINT__/${Oa2print}/g;s/__GAMMA__/"P"/g;s/__IPROP__/${iprop}/g;s/__USEMPS__/0/g;s/__SUBGP__/1/g" ../PROGRAMS/Z_CHIRALVAL_template > ../EXECUTABLES/Z_CHIRALVAL_${beta100}_${e}_${iprop}
			else
			sed "s/__MOMENTUM_ORDER__/$momentum_order/g;s/__BETA100__/$beta100/g;s/__L__/$L/g;s/__T__/$T/g;s/__NCLUST__/$Nclust/g;s/__DIROUTPUT__/$dir_output_doblebarra/g;s/__IMOM__/$imom/g;s/__ENSEMBLE__/${e}/g;s/__PMI__/${pm}/g;s/__Oa2PRINT__/${Oa2print}/g;s/__GAMMA__/"P"/g;s/__IPROP__/${iprop}/g;s/__USEMPS__/1/g;s/__SUBGP__/1/g" ../PROGRAMS/Z_CHIRALVAL_template > ../EXECUTABLES/Z_CHIRALVAL_${beta100}_${e}_${iprop}
			fi
                        root -q -b -l ../EXECUTABLES/Z_CHIRALVAL_${beta100}_${e}_${iprop}
			rm ../EXECUTABLES/Z_CHIRALVAL_${beta100}_${e}_${iprop}

		done

        done # pmList
	done #Oa2print
	fi
	

        echo "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
        echo "  Zq ZA ZV ZS ZT valence chiral fit "
        echo "      average ensemble              "
        echo "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"

	if [ $Do_ZALL_ChiralLimit = "yes" ]
	then
	for Oa2print in ${Oa2List[*]}
        do	
        for pm in "a"
	#for pm in "p" "m" #TEST-PETROS
        do

                if [ $convention = "APE" ]
                then

                dir_output_doblebarra="\/home\/nucave\/workdir\/lattice\/RENORMALIZATION\/DATA\/OUTPUT-APE\/${action}Nf${Nf}\/b$beta100\/Z2F\/${e}${pm}\/"
                else
                dir_output_doblebarra="\/home\/nucave\/workdir\/lattice\/RENORMALIZATION\/DATA\/OUTPUT\/${action}Nf${Nf}\/b$beta100\/Z2F\/${e}${pm}\/"
                fi

                time for imom in `seq 0 $Nmom_eq`
		#for imom in 47 # TEST-PETROS
                do
                        echo "=================================================================="
                        echo "=======>         $Oa2print p=$imom             <=================="

                        for gamma in q S V T A
	#		for gamma in A # TEST-PETROS
                        do
                                sed "s/__MOMENTUM_ORDER__/$momentum_order/g;s/__BETA100__/$beta100/g;s/__L__/$L/g;s/__T__/$T/g;s/__NCLUST__/$Nclust/g;s/__DIROUTPUT__/$dir_output_doblebarra/g;s/__IMOM__/$imom/g;s/__ENSEMBLE__/${e}/g;s/__PMI__/${pm}/g;s/__Oa2PRINT__/${Oa2print}/g;s/__GAMMA__/${gamma}/g;s/__IPROP__/${iprop}/g;;s/__USEMPS__/0/g;s/__SUBGP__/0/g" ../PROGRAMS/Z_CHIRALVAL_template > ../EXECUTABLES/Z_CHIRALVAL_${beta100}_${e}_${iprop}
                                root -q -b -l ../EXECUTABLES/Z_CHIRALVAL_${beta100}_${e}_${iprop} 
			  	rm ../EXECUTABLES/Z_CHIRALVAL_${beta100}_${e}_${iprop}
                        done
                done
         done # pmList
	done #Oa2 list
	fi
	fi # End Do_ChiralVal
	
###((((((((((((((((((((((((())))))))))))))))))))))))))))###
##              p/m average after chiral limit
###((((((((((((((((((((((((())))))))))))))))))))))))))))###

	if [ $Do_prom_pm_chiral = "yes" ] 
	then
       #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
       #@ p/m AVERAGE DIRECTORIES
       #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
       if [ $convention = "APE" ]
       then
                dir_output="$dirdata/DATA/OUTPUT-APE/${action}Nf${Nf}/b$beta100/Z2F/${e}"
       else
                dir_output="$dirdata/DATA/OUTPUT/${action}Nf${Nf}/b$beta100/Z2F/${e}"
       fi
       if [ $democratic_set = "yes" ]
       then
               dir_output_a=${dir_output}a/${list_democratic_pz[$iprop]}${list_democratic_py[$iprop]}${list_democratic_px[$iprop]}${list_democratic_pt[$iprop]}
       else
                dir_output_a=${dir_output}a
       fi

       mkdir -p -v ${dir_output_a}/QAV/Z_ALLP
       for i in `seq 0 $Nmom_eq`; do imom=`printf %03d $i`; mkdir -p -v ${dir_output_a}/QAV/p$imom ; done

       cd $dir/EXECUTABLES/

       for Oa2 in ${Oa2List[*]}
       do
	    args=" -beta100=${beta100} -momentum_order=${momentum_order} -L=$L -T=$T -Nclust=${Nclust} -dir=$dir_output  -set_p=${list_democratic_pz[$iprop]}1,${list_democratic_py[$iprop]}1,${list_democratic_px[$iprop]}1,${list_democratic_pt[$iprop]}1  -iprop=$iprop -gamma_to_prom=P -Oa2=${Oa2} "
	    echo $args
	    time ./Z_PROM_pm_CHIRAL_ZbyZ $args
      done
      fi # END Do_prom_pm 

###((((((((((((((((((((((((())))))))))))))))))))))))))))###
##              Evolve to RI' (a^-1)
## (This is optional, I can do it directly on the sea 
##  extrapolated values) 
###((((((((((((((((((((((((())))))))))))))))))))))))))))###

      if  [ $Do_RGI_valence = "yes" ]
      then
       #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
       #@ p/m AVERAGE DIRECTORIES
       #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

       if [ $convention = "APE" ]
       then
                dir_output="$dirdata/DATA/OUTPUT-APE/${action}Nf${Nf}/b$beta100/Z2F/${e}"
       else
                dir_output="$dirdata/DATA/OUTPUT/${action}Nf${Nf}/b$beta100/Z2F/${e}"
       fi


       cd $dir/EXECUTABLES/

	for Oa2 in ${Oa2List[*]}
	do
	      echo " $Oa2 EVOLUTION  to RI\' at mu=a^-1 	"
	      args=" -beta100=${beta100} -momentum_order=${momentum_order} -L=$L -T=$T -Nclust=${Nclust} -dir=$dir_output  -set_p=${list_democratic_pz[$iprop]}1,${list_democratic_py[$iprop]}1,${list_democratic_px[$iprop]}1,${list_democratic_pt[$iprop]}1  -iprop=$iprop  -ainv=$ainv -Nf=${Nf}  -Oa2=${Oa2}"
	      time ./ZRGI  $args 
	      echo " =============================================="
	done
      fi # END Do_RGI

###((((((((((((((((((((((((())))))))))))))))))))))))))))###
##              p2 fit for each ensemble 
## (Also optional we can do it at the end)
###((((((((((((((((((((((((())))))))))))))))))))))))))))###

      #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
      #@  P2 fit at each ensemble
      #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    
     if [ $Do_p2_val_fit = "yes" ]
     then

       if [ $convention = "APE" ]
       then
                dir_output_doblebarra="\/home\/nucave\/workdir\/lattice\/RENORMALIZATION\/DATA\/OUTPUT-APE\/${action}Nf${Nf}\/b$beta100\/Z2F\/${e}a\/"
       else
                dir_output_doblebarra="\/home\/nucave\/workdir\/lattice\/RENORMALIZATION\/DATA\/OUTPUT\/${action}Nf${Nf}\/b$beta100\/Z2F\/${e}a\/"
       fi

       if [ $convention = "APE" ]
       then
                dir_output="$dirdata/DATA/OUTPUT-APE/${action}Nf${Nf}/b$beta100/Z2F/${e}"
       else
                dir_output="$dirdata/DATA/OUTPUT/${action}Nf${Nf}/b$beta100/Z2F/${e}"
       fi

       if [ $democratic_set = "yes" ]
       then
               dir_output_a=${dir_output}a/${list_democratic_pz[$iprop]}${list_democratic_py[$iprop]}${list_democratic_px[$iprop]}${list_democratic_pt[$iprop]}
       else
                dir_output_a=${dir_output}a
       fi


       for j in 1 2 ; do mkdir -p ${dir_output_a}/QAV/p2FIT/M${j} ; done
	
       for Oa2print in ${Oa2List[*]}
       do
       for rgi in "_RIp_ainv" 
       do
         echo "================================================================================"
	 echo " ==================> $Oa2print $rgi p2FIT 	 <========================"

	 sed "s/__MOMENTUM_ORDER__/$momentum_order/g;s/__BETA100__/$beta100/g;s/__L__/$L/g;s/__T__/$T/g;s/__NCLUST__/$Nclust/g;s/__DIROUTPUT__/$dir_output_doblebarra/g;s/__ENSEMBLE__/${e}/g;s/__PMI__/a/g;s/__Oa2PRINT__/${Oa2print}/g;s/__IPROP__/${iprop}/g;s/__P2MIN_M1__/$p2min_lin/g;s/__P2MIN_M2__/$p2min_av/g;s/__P2MAX_M1__/$p2max_lin/g;s/__P2MAX_M2__/$p2max_av/g;s/__CUT__/$cut/g;s/__RGI__/$rgi/g" ../PROGRAMS/p2FIT_template > ../EXECUTABLES/p2FIT_${beta100}_${e}_${iprop}

        time root -q -b -l ../EXECUTABLES/p2FIT_${beta100}_${e}_${iprop}
	rm ../EXECUTABLES/p2FIT_${beta100}_${e}_${iprop}

	done
	done
     fi # END Do_p2_val_fit


done #eList
#@@@@@@@@@@@@@ END ENSEMBLE LIST

fi # END DO_VALENCE

#####################################
############################
## SEA CHIRAL LIMIT
###########################
####################################

if [ $DO_CHIRAL_SEA = "yes" ]
then

	# Input Output directories
       if [ $convention = "APE" ]
       then
                dir_output="$dirdata/DATA/OUTPUT-APE/${action}Nf${Nf}/b$beta100/Z2F/"
       else
                dir_output="$dirdata/DATA/OUTPUT/${action}Nf${Nf}/b$beta100/Z2F/"
       fi

       if [ $convention = "APE" ]
       then

                dir_output_doblebarra="\/home\/nucave\/workdir\/lattice\/RENORMALIZATION\/DATA\/OUTPUT-APE\/${action}Nf${Nf}\/b$beta100\/Z2F\/"
       else
                dir_output_doblebarra="\/home\/nucave\/workdir\/lattice\/RENORMALIZATION\/DATA\/OUTPUT\/${action}Nf${Nf}\/b$beta100\/Z2F\/"
       fi


       #mkdir -p -v ${dir_output}a/QAV/Z_ALLP
       #for i in `seq 0 $Nmom_eq`; do imom=`printf %03d $i`; mkdir -p -v ${dir_output}a/QAV/p$imom ; done

       mkdir -p ${dir_output}a/${list_democratic_pz[$iprop]}${list_democratic_py[$iprop]}${list_democratic_px[$iprop]}${list_democratic_pt[$iprop]}/QAV/Z_ALLP
       for i in `seq 0 $Nmom_eq`; do imom=`printf %03d $i`; mkdir -p -v ${dir_output}a/${list_democratic_pz[$iprop]}${list_democratic_py[$iprop]}${list_democratic_px[$iprop]}${list_democratic_pt[$iprop]}/QAV/p$imom ; done

	## Input files

        for Oa2print in ${Oa2List[*]}
        do

        if [ $beta100 -eq 190 ]
        then
               sea_ensembleList=("1" "2" "3" "4")
               sea_muList=(0.0080 0.0080 0.0080 0.0080)
               sea_mpcacList=(0.0274 0.03140 0.03570 0.03940)
	       #sea_ensembleList=("A1"  "A3" "A4")
		letter=("A")
	      # sea_ensembleList=(1 3 4)
              # sea_muList=(0.0080 0.0080 0.0080)
              # sea_mpcacList=(0.0274 0.03570 0.03940)
              #  sea_ensembleList=(3 4)
              #  sea_muList=(0.0080 0.0080)
              #  sea_mpcacList=(0.03570 0.03940)



               if [ $Oa2print = "uncor" ] ; then  ZAPREV=0.748 ; fi
	       if [ $Oa2print = "Oa2g2" ] ; then  ZAPREV=0.735 ; fi
	       if [ $Oa2print = "Oa2g2b" ] ; then  ZAPREV=0.725 ; fi

        fi

	if [ $beta100 -eq 195 ]
	then
		letter=("NULL")
   	       sea_ensembleList=(1 2 3 7 8)
	       sea_muList=(0.0085 0.0085 0.0180 0.0085 0.0020)
	       sea_mpcacList=(0.042 0.0200 0.0162 0.0357 0.0363)
		
               #sea_ensembleList=(1 2 3 4 7 8)
               #sea_muList=(0.0085 0.0085  0.0180 0.0085 0.0085 0.0020)
               #sea_mpcacList=(0.042 0.020 0.0162 0.015  0.0357 0.0363)
               #sea_ensembleList=(1 2 3 )
               #sea_muList=(0.0085 0.0085 0.0180 )
               #sea_mpcacList=(0.042 0.0200 0.0162 )

               if [ $Oa2print = "uncor" ] ; then  ZAPREV=0.763 ; fi
               if [ $Oa2print = "Oa2g2" ] ; then  ZAPREV=0.748 ; fi
               if [ $Oa2print = "Oa2g2b" ] ; then  ZAPREV=0.738 ; fi
		

	fi
	if [ $beta100 -eq 210 ]
	then
		letter=("NULL")
                sea_ensembleList=(2 3 4 5)
		sea_muList=(0.0030 0.0046 0.0064 0.0078)
		sea_mpcacList=(0.00410 0.00572 0.00684 0.00822)
       #         sea_ensembleList=(3 4 5)
       #         sea_muList=(0.0046 0.0064 0.0078)
       #         sea_mpcacList=(0.00572 0.00684 0.00822)

               if [ $Oa2print = "uncor" ] ; then  ZAPREV=0.786 ; fi
               if [ $Oa2print = "Oa2g2" ] ; then  ZAPREV=0.775 ; fi
               if [ $Oa2print = "Oa2g2b" ] ; then  ZAPREV=0.768 ; fi

	fi
	
        seaNmass=${#sea_muList[*]}

        echo $seaNmass > $dirinput/masses_${beta100}.dat
	echo $letter >> $dirinput/masses_${beta100}.dat
        for im in `seq 1 $seaNmass`; do ensemble=${sea_ensembleList[$(echo " $im -1 " |bc -l )]}; echo $ensemble >> $dirinput/masses_${beta100}.dat; done
        for im in `seq 1 $seaNmass`; do  mass=${sea_muList[$(echo " $im -1 " |bc -l )]}; echo $mass >> $dirinput/masses_${beta100}.dat; done
	for im in `seq 1 $seaNmass`; do mPCAC=${sea_mpcacList[$(echo " $im -1 " |bc -l )]}; echo $mPCAC >> $dirinput/masses_${beta100}.dat; done
        echo $ZAPREV >> $dirinput/masses_${beta100}.dat
	echo "  --> $dirinput/masses_${beta100}.dat  file written"

	for rgiprint in ${rgiList[*]}
	do

                time for imom in `seq 0 $Nmom_eq`
                do
                        echo "=================================================================="
                        echo "=============>         $Oa2print p=$imom             <=================="
                        sed "s/__MOMENTUM_ORDER__/$momentum_order/g;s/__BETA100__/$beta100/g;s/__L__/$L/g;s/__T__/$T/g;s/__NCLUST__/$Nclust/g;s/__DIROUTPUT__/$dir_output_doblebarra/g;s/__IMOM__/$imom/g;s/__PMI__/a/g;s/__Oa2PRINT__/${Oa2print}/g;s/__RGIPRINT__/$rgi/g;s/__IPROP__/${iprop}/g" ../PROGRAMS/Z_CHIRALSEA_template > ../EXECUTABLES/Z_CHIRALSEA_${beta100}_${iprop}
                        root -q -b -l ../EXECUTABLES/Z_CHIRALSEA_${beta100}_${iprop}
                        rm ../EXECUTABLES/Z_CHIRALSEA_${beta100}_${iprop}

		done
	done
	done	
fi

if  [ $DO_PROM_EVEN_DEMO = "yes" ]
then
	iprop=16
       #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
       #@ p/m AVERAGE DIRECTORIES
       #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

       if [ $convention = "APE" ]
       then
                dir_output="$dirdata/DATA/OUTPUT-APE/${action}Nf${Nf}/b$beta100/Z2F/"
       else
                dir_output="$dirdata/DATA/OUTPUT/${action}Nf${Nf}/b$beta100/Z2F/"
       fi


	mkdir -p -v $dir_output/a/000+/QAV/Z_ALLP
       for i in `seq 0 $Nmom_eq`; do imom=`printf %03d $i`; mkdir -p -v ${dir_output}a/000+/QAV/p$imom ; done
 

       cd $dir/EXECUTABLES/

        for Oa2 in ${Oa2List[*]}
        do
              args=" -beta100=${beta100} -momentum_order=${momentum_order} -L=$L -T=$T -Nclust=${Nclust} -dir=$dir_output -Nprop=8 -output_name=000+  -list_prop=0,2,4,6,8,10,12,14  -Oa2=${Oa2}"
              time ./Z_PROM_DEMO  $args
              echo " =============================================="
        done




fi 


if  [ $DO_RGI = "yes" ]
then
       #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
       #@ p/m AVERAGE DIRECTORIES
       #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

       if [ $convention = "APE" ]
       then
                dir_output="$dirdata/DATA/OUTPUT-APE/${action}Nf${Nf}/b$beta100/Z2F/"
       else
                dir_output="$dirdata/DATA/OUTPUT/${action}Nf${Nf}/b$beta100/Z2F/"
       fi


       cd $dir/EXECUTABLES/

        for Oa2 in ${Oa2List[*]}
        do
              echo " $Oa2 EVOLUTION  to RI\' at mu=a^-1         "
	      if [ $DO_PROM_EVEN_DEMO = "yes" ]
	      then
		#iprop=16 -> EVEN --> 000+
              args=" -beta100=${beta100} -momentum_order=${momentum_order} -L=$L -T=$T -Nclust=${Nclust} -dir=$dir_output  -set_p=1,1,1,1  -iprop=${iprop}  -ainv=$ainv -Nf=${Nf}  -Oa2=${Oa2}"
	      else
              args=" -beta100=${beta100} -momentum_order=${momentum_order} -L=$L -T=$T -Nclust=${Nclust} -dir=$dir_output  -set_p=${list_democratic_pz[$iprop]}1,${list_democratic_py[$iprop]}1,${list_democratic_px[$iprop]}1,${list_democratic_pt[$iprop]}1  -iprop=$iprop  -ainv=$ainv -Nf=${Nf}  -Oa2=${Oa2}"
	      fi
              time ./ZRGI  $args
              echo " =============================================="
        done




fi # END Do_RGI


#############################################
#############################
## p2 fit 
#############################
#######################################
if [ $DO_P2FIT = "yes" ]
then

       if [ $convention = "APE" ]
       then
                dir_output_doblebarra="\/home\/nucave\/workdir\/lattice\/RENORMALIZATION\/DATA\/OUTPUT-APE\/${action}Nf${Nf}\/b$beta100\/Z2F\/a\/"
       else
                dir_output_doblebarra="\/home\/nucave\/workdir\/lattice\/RENORMALIZATION\/DATA\/OUTPUT\/${action}Nf${Nf}\/b$beta100\/Z2F\/a\/"
       fi

       if [ $convention = "APE" ]
       then
                dir_output="$dirdata/DATA/OUTPUT-APE/${action}Nf${Nf}/b$beta100/Z2F/"
       else
                dir_output="$dirdata/DATA/OUTPUT/${action}Nf${Nf}/b$beta100/Z2F/"
       fi

       if [ $democratic_set = "yes" ]
       then
              if [ $DO_PROM_EVEN_DEMO = "yes" ]
              then
	       dir_output_a=${dir_output}a/000+
	      else	
               dir_output_a=${dir_output}a/${list_democratic_pz[$iprop]}${list_democratic_py[$iprop]}${list_democratic_px[$iprop]}${list_democratic_pt[$iprop]}
	       fi
       else
               dir_output_a=${dir_output}a
       fi


       for j in 1 2 ; do mkdir -p ${dir_output_a}/QAV/p2FIT/M${j} ; done


       for Oa2print in ${Oa2List[*]}
       do
       for rgi in "_RIp_ainv"
       do
         echo "================================================================================"
         echo " ==================> $Oa2print $rgi p2FIT         <========================"

         sed "s/__MOMENTUM_ORDER__/$momentum_order/g;s/__BETA100__/$beta100/g;s/__L__/$L/g;s/__T__/$T/g;s/__NCLUST__/$Nclust/g;s/__DIROUTPUT__/$dir_output_doblebarra/g;s/__ENSEMBLE__/${e}/g;s/__PMI__/a/g;s/__Oa2PRINT__/${Oa2print}/g;s/__IPROP__/${iprop}/g;s/__P2MIN_M1__/$p2min_lin/g;s/__P2MIN_M2__/$p2min_av/g;s/__P2MAX_M1__/$p2max_lin/g;s/__P2MAX_M2__/$p2max_av/g;s/__CUT__/$cut/g;s/__RGI__/$rgi/g" ../PROGRAMS/p2FIT_template > ../EXECUTABLES/p2FIT_${beta100}_${e}_${iprop}

        time root -q -b -l ../EXECUTABLES/p2FIT_${beta100}_${e}_${iprop}
        rm ../EXECUTABLES/p2FIT_${beta100}_${e}_${iprop}

        done
        done

fi

if [ $beta100 -eq 190 ]
then
musigma=0.15
mudelta=0.19
CP_RIp2MS=1.20393
fi
if [ $beta100 -eq 195 ]
then
musigma=0.135
mudelta=0.17
CP_RIp2MS=1.14711

fi
if [ $beta100 -eq 210 ]
then
musigma=0.12
mudelta=0.1385
CP_RIp2MS=1.05891
fi

if [ $DO_RENO_SEA_QUARK_MASS = "yes" ]
then

       if [ $convention = "APE" ]
       then
                dir_output="$dirdata/DATA/OUTPUT-APE/${action}Nf${Nf}/b$beta100/Z2F/a"
       else
                dir_output="$dirdata/DATA/OUTPUT/${action}Nf${Nf}/b$beta100/Z2F/a"
       fi
	
      if  [ $beta100 -eq 190 ]
      then
	for im in 1 2 ; do mkdir -p -v $dir_output/000+/QAV/QUARK_MASS/M$im ; done

      else
        for im in 1 2 ; do mkdir -p  -v $dir_output/++++/QAV/QUARK_MASS/M$im ; done 

      fi


       cd $dir/EXECUTABLES/

       for Oa2 in ${Oa2List[*]}
       do
       for rgi in "_RIp_ainv"
       do
		./RenormalizeQuarkMass -dir=${dir_output}/$demo_set/ -Nclust=${Nclust}  -iprop=${iprop}  -ainv=$ainv  -Oa2=${Oa2} -rgi=${rgi} -mudelta=${mudelta} -musigma=${musigma}
       done
       done

fi

cd $dir

